﻿////==============================================================================  
////Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
////GUID：46172595-6b69-4526-b9da-426d663f5b05
////CLR Version: 4.0.30319.18033
////Code Author：Kevin Wang
////Contact：Email(Admin@9un.org),QQ(265382 or 74344)
////Filename：DoubleWindowBase
////Namespace：RuanRang.Media.Core
////Functions：DoubleWindowBase  
////Created by Kevin Wang at 2013/2/2 23:05:09 http://blog.9un.org
////============================================================================== 
//using System;
//using System.ComponentModel;
//using System.Threading;
//using RuanRang.Media.Core;

//namespace RuanRang.Media.WinForms
//{
//    /// <summary>
//    /// Event receiver which wraps events and synchronizes them with the main thread of Windows.Forms application.
//    /// </summary>
//    public sealed class SynchronizedEventsReceiver : PlayerEventsReceiver
//    {
//        private readonly bool _useThreadPool;
//        private readonly PlayerEventsReceiver _delegateTo;
//        private readonly ISynchronizeInvoke _invoker;

//        /// <summary>
//        /// Instantiates events receiver with specified parameters.
//        /// </summary>
//        /// <param name="invoker"><see cref="ISynchronizeInvoke" /> used to synchronize with.</param>
//        /// <param name="delegateTo"><see cref="PlayerEventsReceiver" /> to delegate invokes to</param>
//        /// <param name="useThreadPool">Set this value to <code>true</code> to wrap synchronization calls with <see cref="ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback)" />.
//        /// This maybe necessary to reduce delays during events.</param>
//        /// <exception cref="System.ArgumentNullException">
//        /// invoker
//        /// or
//        /// delegateTo
//        /// </exception>
//        public SynchronizedEventsReceiver(ISynchronizeInvoke invoker, PlayerEventsReceiver delegateTo, bool useThreadPool)
//        {
//            if (invoker == null)
//            {
//                throw new ArgumentNullException("invoker");
//            }
//            if (delegateTo == null)
//            {
//                throw new ArgumentNullException("delegateTo");
//            }
//            _invoker = invoker;
//            _delegateTo = delegateTo;
//            _useThreadPool = useThreadPool;
//        }

//        /// <summary>
//        /// Does the invoke.
//        /// </summary>
//        /// <param name="handler">The handler.</param>
//        /// <param name="parameters">The parameters.</param>
//        private void DoInvoke(Delegate handler, object[] parameters)
//        {
//            _invoker.BeginInvoke(handler, parameters);
//        }

//        /// <summary>
//        /// Some error occurred.
//        /// </summary>
//        public override void OnEncounteredError()
//        {
//            if (_useThreadPool)
//            {
//                ThreadPool.QueueUserWorkItem(OnEncounteredErrorInternal);
//            }
//            else
//            {
//                OnEncounteredErrorInternal(null);
//            }
//        }

//        /// <summary>
//        /// Called when [encountered error internal].
//        /// </summary>
//        /// <param name="param">The param.</param>
//        private void OnEncounteredErrorInternal(object param)
//        {
//            if (_invoker.InvokeRequired)
//            {
//                DoInvoke(new Action<Object>(OnEncounteredErrorInternal), new[] {
//                    param
//                });
//            }
//            else
//            {
//                _delegateTo.OnEncounteredError();
//            }
//        }

//        /// <summary>
//        /// End of media reached.
//        /// </summary>
//        public override void OnEndReached()
//        {
//            if (_useThreadPool)
//            {
//                ThreadPool.QueueUserWorkItem(OnEndReachedInternal);
//            }
//            else
//            {
//                OnEndReachedInternal(null);
//            }
//        }

//        /// <summary>
//        /// Called when [end reached internal].
//        /// </summary>
//        /// <param name="param">The param.</param>
//        private void OnEndReachedInternal(object param)
//        {
//            if (_invoker.InvokeRequired)
//            {
//                DoInvoke(new Action<Object>(OnEndReachedInternal), new[] {
//                    param
//                });
//            }
//            else
//            {
//                _delegateTo.OnEndReached();
//            }
//        }

//        /// <summary>
//        /// Position of player is changed.
//        /// </summary>
//        public override void OnPositionChanged()
//        {
//            if (_useThreadPool)
//            {
//                ThreadPool.QueueUserWorkItem(OnPositionChangedInternal);
//            }
//            else
//            {
//                OnPositionChangedInternal(null);
//            }
//        }

//        /// <summary>
//        /// Called when [position changed internal].
//        /// </summary>
//        /// <param name="param">The param.</param>
//        private void OnPositionChangedInternal(object param)
//        {
//            if (_invoker.InvokeRequired)
//            {
//                DoInvoke(new Action<Object>(OnPositionChangedInternal), new[] {
//                    param
//                });
//            }
//            else
//            {
//                _delegateTo.OnPositionChanged();
//            }
//        }

//        /// <summary>
//        /// State of player changed.
//        /// </summary>
//        public override void OnStateChanged()
//        {
//            if (_useThreadPool)
//            {
//                ThreadPool.QueueUserWorkItem(OnStateChangedInternal);
//            }
//            else
//            {
//                OnStateChangedInternal(null);
//            }
//        }

//        /// <summary>
//        /// Called when [state changed internal].
//        /// </summary>
//        /// <param name="param">The param.</param>
//        private void OnStateChangedInternal(object param)
//        {
//            if (_invoker.InvokeRequired)
//            {
//                DoInvoke(new Action<Object>(OnStateChangedInternal), new[] {
//                    param
//                });
//            }
//            else
//            {
//                _delegateTo.OnStateChanged();
//            }
//        }

//        /// <summary>
//        /// Player is stopped.
//        /// </summary>
//        public override void OnStopped()
//        {
//            if (_useThreadPool)
//            {
//                ThreadPool.QueueUserWorkItem(OnStoppedInternal);
//            }
//            else
//            {
//                OnStoppedInternal(null);
//            }
//        }

//        /// <summary>
//        /// Called when [stopped internal].
//        /// </summary>
//        /// <param name="param">The param.</param>
//        private void OnStoppedInternal(object param)
//        {
//            if (_invoker.InvokeRequired)
//            {
//                DoInvoke(new Action<Object>(OnStoppedInternal), new[] {
//                    param
//                });
//            }
//            else
//            {
//                _delegateTo.OnStopped();
//            }
//        }

//        /// <summary>
//        /// Time of player changed.
//        /// </summary>
//        public override void OnTimeChanged()
//        {
//            if (_useThreadPool)
//            {
//                ThreadPool.QueueUserWorkItem(OnTimeChangedInternal);
//            }
//            else
//            {
//                OnTimeChangedInternal(null);
//            }
//        }

//        /// <summary>
//        /// Called when [time changed internal].
//        /// </summary>
//        /// <param name="param">The param.</param>
//        private void OnTimeChangedInternal(object param)
//        {
//            if (_invoker.InvokeRequired)
//            {
//                DoInvoke(new Action<Object>(OnTimeChangedInternal), new[] {
//                    param
//                });
//            }
//            else
//            {
//                _delegateTo.OnTimeChanged();
//            }
//        }
//    }
//}
